import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'dart:convert';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import './search_result.dart';

class HotWord extends StatefulWidget {
  final Widget child;

  HotWord({Key key, this.child}) : super(key: key);

  _HotWordState createState() => _HotWordState();
}

class _HotWordState extends State<HotWord> {
  List hotData = [];// 存储热门搜索词数据

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getHotWord();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
       width: ScreenUtil().setWidth(700),
       padding: EdgeInsets.all(8),
       child: Wrap(
         children: hotData.map((i){// 通过 map 方法循环创建热门搜索词
           return _word(i);
         }).toList(),
       ),
    );
  }

  Widget _word(i){
    return InkWell(
      child: Container(
        height: 30,
        padding: EdgeInsets.all(5),
        margin: EdgeInsets.fromLTRB(0, 0, 10, 10),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10),
          color: Theme.of(context).primaryColor
        ),
        child: Text(i['word'],style: TextStyle(color: Colors.white),),
      ),
      onTap: (){
        Navigator.push(context, MaterialPageRoute(
          builder: (context){
            return SearchResult(keyword: i['word']);
          }
        ));
      }
    );
  }

  // 获取搜索热词
  void _getHotWord() async {
    Dio dio = new Dio();
    Response response = await dio.get('http://dianshan.free.idcfengye.com/goods/gethotword');
    var data = json.decode(response.toString());
    hotData = data['hotword'];
    setState(() {
      
    });
  }
}